﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Me.Prettyprint.Cassandra.Utils
{
    public enum TimeUnit
    {
        NANOSECONDS, MILLISECONDS, SECONDS
    }
    public class Timer
    {
        public Timer(Metric m)
        {
            Metric = m;
            DurationUnit = TimeUnit.NANOSECONDS;
        }
        public Metric Metric
        {
            get;
            private set;
        }
        public TimeUnit DurationUnit
        {
            get;
            set;
        }
        public TimerContext Time()
        {
            return new TimerContext(this);
        }
        public void Update(long duration, TimeUnit unit)
        {
            if (unit == TimeUnit.SECONDS)
            {
                // convert to nanoseconds
                duration *= 1000000000;
            }
            else if (unit == TimeUnit.MILLISECONDS)
            {
                duration *= 1000;
            }
            Metric.UpdateMetric(duration);
        }
    }
}
